package com.sinodata.bsm.cicp.collector.tomcat;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class GarbageCollecotr extends Collector {
    private static Logger logger = Logger.getLogger(GarbageCollecotr.class);

    @Override
    public List collect(String ip) throws CollectorException {
        //        String port = this.getParamMap().get("端口");
        String port = this.getParamMap().get("Port");
        //        String garbageName = this.getParamMap().get("名称").trim();
        String garbageName = this.getParamMap().get("Name").trim();
        String[] propertyNames = new String[] { "Name", "CollectionCount", "CollectionTime", "Valid" };
        Object[] attrValues = null;

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];

        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName garbage = new ObjectName("java.lang:type=GarbageCollector,*");
            Set<ObjectName> set = connection.queryNames(garbage, null);
            for (ObjectName objectName : set) {
                attrValues = JmxUtil.getAttribute(connection, objectName, propertyNames);
                if (attrValues[0].toString().equals(garbageName)) {
                    String[] temp = new String[attrValues.length - 1];
                    for (int i = 1; i < attrValues.length; i++) {
                        temp[i - 1] = attrValues[i].toString();
                        propValues[i - 1] = ResPropertyValue.createPropValue(temp[i - 1].toString(), propMetas[i - 1]);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("when GarbageCollecotr" + e);
            throw new CollectorException(e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "名称", "端口" };
        return new String[] { "Name", "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "收集次数", "累计收集时间", "是否有效" };
        return new String[] { "$1", "$2", "$3" };
    }

}
